<<<<<<< HEAD
<?php

namespace app\api\services;

use think\Cache;
use think\cache\Driver;
use think\Config;
use think\Exception;
use Throwable;

class CacheServices
{
    /**
     * 过期时间
     * @var int
     */
    protected static $expire;

    /**
     * 写入缓存
     * @param string $name
     * @param $value
     * @param int $expire
     * @param string $tag
     * @return bool
     */
    public static function set(string $name, $value, int $expire, string $tag = 'car')
    {
        try {
            return Cache::tag($tag)->set($name, $value, $expire);
        } catch (Throwable $e) {
            return false;
        }
    }

    /**
     * 如果不存在则写入缓存
     * @param string $name
     * @param $default
     * @param int $expire
     * @param string $tag
     * @return mixed|string
     */
    public static function remember(string $name, $default = '', int $expire = 0, string $tag = 'car')
    {
        try {
            return Cache::tag($tag)->remember($name, $default, $expire);
        } catch (Throwable $e) {
            if (is_callable($default)) {
                return $default();
            } else {
                return $default;
            }
        }
    }

    /**
     * 读取缓存
     * @param string $name
     * @param $default
     * @return mixed|string
     */
    public static function get(string $name, $default = '')
    {
        return Cache::get($name) ?? $default;
    }

    /**
     * 删除缓存
     * @param string $name
     * @return bool
     */
    public static function delete(string $name): bool
    {
        return Cache::rm($name);
    }

    /**
     * 清空缓池
     * @param string $tag
     * @return bool
     */
    public static function clear(string $tag = 'car'): bool
    {
        return Cache::tag($tag)->clear();
    }

    /**
     * 清空全部缓存
     * @return bool
     */
    public static function clearAll(): bool
    {
        return Cache::clear();
    }

    /**
     * 检查缓存是否存在
     * @param string $key
     * @return bool
     */
    public static function has(string $key): bool
    {
        try {
            return Cache::has($key);
        } catch (Exception $e) {
            return false;
        }
    }

    /**
     * 指定缓存类型
     * @param string $type
     * @param string $tag
     * @return object|Driver
     */
    public static function store(string $type = 'file', string $tag = 'car')
    {
        return Cache::store($type)->tag($tag);
    }

    public static function lock($name, $value, $expire = 0)
    {
        if (!self::has($name)) {
            self::set($name, $value, $expire);
            return true;
        } else {
            return false;
        }
    }

    /**
     * 检查锁
     * @param string $key
     * @param int $timeout
     * @return bool
     */
    public static function setMutex(string $key, int $timeout = 10)
    {
        $curTime = time();
        $readMutexKey = "redis:mutex:{$key}";
        $mutexRes = Cache::store('redis')->handler()->setnx($readMutexKey, $curTime + $timeout);
        if ($mutexRes) {
            return true;
        }
        //就算意外退出，下次进来也会检查key，防止死锁
        $time = Cache::store('redis')->handler()->get($readMutexKey);
        if ($curTime > $time) {
            Cache::store('redis')->handler()->del($readMutexKey);
            return Cache::store('redis')->handler()->setnx($readMutexKey, $curTime + $timeout);
        }
        return false;
    }

=======
<?php

namespace app\api\services;

use think\Cache;
use think\cache\Driver;
use think\Config;
use think\Exception;
use Throwable;

class CacheServices
{
    /**
     * 过期时间
     * @var int
     */
    protected static $expire;

    /**
     * 写入缓存
     * @param string $name
     * @param $value
     * @param int $expire
     * @param string $tag
     * @return bool
     */
    public static function set(string $name, $value, int $expire, string $tag = 'car')
    {
        try {
            return Cache::tag($tag)->set($name, $value, $expire);
        } catch (Throwable $e) {
            return false;
        }
    }

    /**
     * 如果不存在则写入缓存
     * @param string $name
     * @param $default
     * @param int $expire
     * @param string $tag
     * @return mixed|string
     */
    public static function remember(string $name, $default = '', int $expire = 0, string $tag = 'car')
    {
        try {
            return Cache::tag($tag)->remember($name, $default, $expire);
        } catch (Throwable $e) {
            if (is_callable($default)) {
                return $default();
            } else {
                return $default;
            }
        }
    }

    /**
     * 读取缓存
     * @param string $name
     * @param $default
     * @return mixed|string
     */
    public static function get(string $name, $default = '')
    {
        return Cache::get($name) ?? $default;
    }

    /**
     * 删除缓存
     * @param string $name
     * @return bool
     */
    public static function delete(string $name): bool
    {
        return Cache::rm($name);
    }

    /**
     * 清空缓池
     * @param string $tag
     * @return bool
     */
    public static function clear(string $tag = 'car'): bool
    {
        return Cache::tag($tag)->clear();
    }

    /**
     * 清空全部缓存
     * @return bool
     */
    public static function clearAll(): bool
    {
        return Cache::clear();
    }

    /**
     * 检查缓存是否存在
     * @param string $key
     * @return bool
     */
    public static function has(string $key): bool
    {
        try {
            return Cache::has($key);
        } catch (Exception $e) {
            return false;
        }
    }

    /**
     * 指定缓存类型
     * @param string $type
     * @param string $tag
     * @return object|Driver
     */
    public static function store(string $type = 'file', string $tag = 'car')
    {
        return Cache::store($type)->tag($tag);
    }

    public static function lock($name, $value, $expire = 0)
    {
        if (!self::has($name)) {
            self::set($name, $value, $expire);
            return true;
        } else {
            return false;
        }
    }

    /**
     * 检查锁
     * @param string $key
     * @param int $timeout
     * @return bool
     */
    public static function setMutex(string $key, int $timeout = 10)
    {
        $curTime = time();
        $readMutexKey = "redis:mutex:{$key}";
        $mutexRes = Cache::store('redis')->handler()->setnx($readMutexKey, $curTime + $timeout);
        if ($mutexRes) {
            return true;
        }
        //就算意外退出，下次进来也会检查key，防止死锁
        $time = Cache::store('redis')->handler()->get($readMutexKey);
        if ($curTime > $time) {
            Cache::store('redis')->handler()->del($readMutexKey);
            return Cache::store('redis')->handler()->setnx($readMutexKey, $curTime + $timeout);
        }
        return false;
    }
>>>>>>> 0591f0f6f4619c1e24959052aec247fc0f331c0b
}